package edu.sctu.yueding.activitys;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.adapter.MySignInAdapter;
import edu.sctu.yueding.bean.JsonBaseListBean;
import edu.sctu.yueding.bean.MySignInBean;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.config.URLConfig;
import edu.sctu.yueding.utils.DateUtils;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.ProgressUtil;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;
import edu.sctu.yueding.utils.httputils.SharePreference;
import edu.sctu.yueding.view.XListView;

/**
 * Created by fll on 2016/8/3.
 */
public class MySignInActivity extends BaseActivity implements XListView.IXListViewListener, View.OnClickListener, AdapterView.OnItemClickListener {

    private XListView listView;
    private MySignInAdapter adapter;
    private Activity mActivity = this;
    private TextView mBack;

    private List<MySignInBean> datas = new ArrayList<>();
    private Handler handler = new Handler();
    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;

    private boolean isAddAll = true;
    private int page = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_center_my_signin);
        initView();
        initSystemBar(this, 0);
    }

    private void initView() {

        listView = (XListView) findViewById(R.id.id_my_sign_up_list_view);

        listView.setPullLoadEnable(false);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(this);
        listView.setOnItemClickListener(this);

        mBack = (TextView) findViewById(R.id.id_my_sign_in_back_txt);
        Log.e("sddd", SharePreference.getInstance(this).getString(CacheConfig.CACHE_TOKEN));

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        progressUtil = ProgressUtil.getInstance(this, null);
        progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_SPINNER, true);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                initData();

                if (datas == null) {
                    Toast.makeText(mActivity, "暂无数据", Toast.LENGTH_SHORT).show();
                }
                progressUtil.dismiss(progressDialog);

            }

        }, 1000);

    }

    private void initData() {

        GetVolleyHttp.getInstance(this).getStringGETJsonData(URLConfig.MY_SIGN_UP, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                try {

                    JsonBaseListBean<MySignInBean> jsonBaseListBean = JSON.parseObject(response, new TypeReference<JsonBaseListBean<MySignInBean>>() {
                    }.getType());

                    Log.e("ter", response);

                    if (jsonBaseListBean.getMeta().code == 200) {

                        datas = jsonBaseListBean.getData();
                        initAdapter();
                    }
                } catch (JSONException exception) {

                    Toast.makeText(mActivity, "网络请求失败", Toast.LENGTH_SHORT).show();
                }

                progressUtil.dismiss(progressDialog);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }, "Bearer " + SharePreference.getInstance(this).getString(CacheConfig.CACHE_TOKEN));
    }

    @Override
    public void onRefresh() {
        page = 0;
        isAddAll = false;
        initData();
    }

    @Override
    public void onLoadMore() {
        isAddAll = true;
        initData();
    }


    private void initAdapter() {

        if (adapter == null) {
            adapter = new MySignInAdapter(mActivity, datas);
            listView.setAdapter(adapter);
        } else {

            adapter.fresh(datas, isAddAll);
        }
        stop();

    }

    private void stop() {
        listView.stopLoadMore();
        listView.stopRefresh();
        listView.setRefreshTime(DateUtils.getTime());
    }


    @Override
    public void onClick(View view) {

    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


        MySignInBean mySignInBean = (MySignInBean) adapter.getItem(i - 1);

        Bundle bundle = new Bundle();

        bundle.putLong("matchId", mySignInBean.getId());

        Log.e("555", String.valueOf(mySignInBean.getId()));

        JumpUtils.jumpActivity(MySignInActivity.this, MyTeamActivity.class, bundle);

    }
}
